﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HtmlAgilityPack;
using System.Text.RegularExpressions;

namespace OlapProject
{
    class Prian : IParser
    {

        HtmlWeb http = new HtmlWeb();
        const string FIRST_PAGE = "http://prian.eu/search/?type%5B%5D=1&object=100&country%5B%5D=553&country%5B%5D=528&country%5B%5D=43&country%5B%5D=559&country%5B%5D=52&country%5B%5D=487&country%5B%5D=290&country%5B%5D=49&country%5B%5D=208&country%5B%5D=461&country%5B%5D=50&country%5B%5D=284&country%5B%5D=283&country%5B%5D=482&country%5B%5D=38&country%5B%5D=292&country%5B%5D=6&country%5B%5D=288&country%5B%5D=224&country%5B%5D=542&country%5B%5D=5&country%5B%5D=42&country%5B%5D=46&country%5B%5D=71&country%5B%5D=287&country%5B%5D=591&country%5B%5D=51&country%5B%5D=44&country%5B%5D=566&country%5B%5D=7&country%5B%5D=193&country%5B%5D=41&country%5B%5D=45&country%5B%5D=48&country%5B%5D=574&country%5B%5D=47&minprice=&maxprice=&search_currency=2";
        //dla testu , last page
        //const string FIRST_PAGE = "http://prian.eu/search/?country[]=553&country[]=528&country[]=43&country[]=559&country[]=52&country[]=487&country[]=290&country[]=49&country[]=208&country[]=461&country[]=50&country[]=284&country[]=283&country[]=482&country[]=38&country[]=292&country[]=6&country[]=288&country[]=224&country[]=542&country[]=5&country[]=42&country[]=46&country[]=71&country[]=287&country[]=591&country[]=51&country[]=44&country[]=566&country[]=7&country[]=193&country[]=41&country[]=45&country[]=48&country[]=574&country[]=47&type[]=1&parent=100&search_currency=2&acountry[1]=553&acountry[2]=528&acountry[3]=43&acountry[4]=559&acountry[5]=52&acountry[6]=487&acountry[7]=290&acountry[8]=49&acountry[9]=208&acountry[10]=461&acountry[11]=50&acountry[12]=284&acountry[13]=283&acountry[14]=482&acountry[15]=38&acountry[16]=292&acountry[17]=6&acountry[18]=288&acountry[19]=224&acountry[20]=542&acountry[21]=5&acountry[22]=42&acountry[23]=46&acountry[24]=71&acountry[25]=287&acountry[26]=591&acountry[27]=51&acountry[28]=44&acountry[29]=566&acountry[30]=7&acountry[31]=193&acountry[32]=41&acountry[33]=45&acountry[34]=48&acountry[35]=574&acountry[36]=47&order=2&orderby=3&next=11850";
        HtmlDocument paginationPage;
        
        //Xpath wybierajacy linki, powinno byc 5 
        const string XPATH_ITEMS = "//dl[@class=\"info\"]//a[contains(concat( \" \", @class, \" \" ),concat( \" \", \"block\", \" \" ))]";
        //Xpath wybierajacy nastepna strone
        const string XPATH_NEXT_PAGE = "//a[@class=\"pagenavigation\"][2]";

        private List<PropertyVO> data;

        public Prian () 
        {
            data = new List<PropertyVO>();
        }

        private void init()
        {
            paginationPage = http.Load(FIRST_PAGE);
            data.Clear();
        }

        public List<PropertyVO> getData()
        {
            Log.writeLine("Prian STARTING ...");

            init();       
            List<PropertyVO> result;
            string link;
            int i = 0;


            while (hasNextLink())
            {
                link = getNextLink();
                result = getParsedPages(link);
                data = data.Concat(result).ToList();
                Console.Out.WriteLine(i);
                i++;

                Log.parserLog(i, "Imlix");
                if (DebugModel.debugMode && i >= DebugModel.MAX_PAGE_IN_DEBUG)
                    break;
            }

            return data;
        }
        public PropertyVO parsePage(string link)
        {
            PrianPageParser parser = new PrianPageParser(link, http);
            PropertyVO data = parser.getData();

            return data;
        }
        public List<PropertyVO> getParsedPages(string linkToList)
        {
            paginationPage = http.Load(linkToList);
            HtmlNodeCollection linksToProperties = paginationPage.DocumentNode.SelectNodes(XPATH_ITEMS);
   
            List<PropertyVO> parsedPages = new List<PropertyVO>();

            if (linksToProperties == null)
            {
                Log.writeLine("Prian: Pagie is unavailable");
                return parsedPages;
            }
           
            foreach (HtmlNode link in linksToProperties)
            {
                parsedPages.Add(parsePage(link.Attributes["href"].Value));
            }

            return parsedPages;
        }

        public bool hasNextLink()
        {
            HtmlNodeCollection pagination = paginationPage.DocumentNode.SelectNodes("//div[@class=\"page\"]//strong");

            if (pagination == null) return false;
            int i = -1;
            int currentPage = 0, allPages = 0;

            string currentPageStr = Regex.Replace(pagination[0].InnerText, "[^\\d]", "");
            string allPagesStr = pagination[1].InnerText;

            if (Int32.TryParse(currentPageStr, out i))
            {
                currentPage = i;
            }
            if (Int32.TryParse(allPagesStr, out i))
            {
                allPages = i;
            }
            
            return currentPage < allPages;
        }

        public String getNextLink()
        {
            return "http://prian.eu" + paginationPage.DocumentNode.SelectSingleNode(XPATH_NEXT_PAGE).Attributes["href"].Value;
        }
    }
}
